#!/bin/bash

# 优化的RTSP推流脚本 - 解决VLC播放卡顿问题
# 关键优化:
# 1. 添加 -use_wallclock_as_timestamps 1 修复时间戳
# 2. 添加 -fflags +genpts 生成PTS
# 3. 添加 -max_delay 设置最大延迟
# 4. 添加 -rtsp_flags prefer_tcp 优化TCP传输

# 视频文件目录
VIDEO_DIR="./"

# RTSP服务器地址
RTSP_SERVER="0.0.0.0:10554"  # 使用远程RTSP服务器

# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

echo "=========================================="
echo "  优化的RTSP推流脚本"
echo "  解决VLC播放卡顿问题"
echo "=========================================="
echo ""

# 定义推流配置 (格式: "视频文件:流名称")
streams=(
    "traffic.mp4:traffic_mp4"
    "vehicle_count.mp4:vehicle_count_mp4"
    "vehicle_count1.mp4:vehicle_count1_mp4"
    "vehicle_count_fixed.mp4:vehicle_count_fixed_mp4"
    "sample_1080p.mp4:sample_1080p_mp4"
)

# 优化的ffmpeg推流命令
start_stream() {
    local video_file=$1
    local stream_name=$2
    local video_path="${VIDEO_DIR}/${video_file}"
    local rtsp_url="rtsp://${RTSP_SERVER}/live/${stream_name}"
    
    if [ ! -f "$video_path" ]; then
        echo -e "${RED}错误: 视频文件不存在: $video_path${NC}"
        return 1
    fi
    
    echo -e "${GREEN}启动推流: $stream_name${NC}"
    echo "  视频: $video_file"
    echo "  URL: $rtsp_url"
    
    # 优化的推流命令
    export LD_LIBRARY_PATH=/opt/tops/lib
    ffmpeg -re \
        -stream_loop -1 \
        -fflags +genpts \
	-avoid_negative_ts make_zero \
        -i "$video_path" \
        -c:v copy \
        -an \
        -f rtsp \
        -rtsp_transport tcp \
	-rtsp_flags prefer_tcp \
        -max_delay 500000 \
        -muxdelay 0.1 \
        "$rtsp_url" \
        > /tmp/rtsp_${stream_name}.log 2>&1 &
    
    echo "  PID: $!"
    echo ""
}

# 检查视频文件
echo "检查视频文件..."
for stream_config in "${streams[@]}"; do
    IFS=':' read -r video_file stream_name <<< "$stream_config"
    if [ ! -f "${VIDEO_DIR}/${video_file}" ]; then
        echo -e "${RED}错误: ${video_file} 不存在${NC}"
        exit 1
    fi
    echo "  ✓ ${video_file}"
done
echo ""

# 启动所有流
echo "启动推流..."
for stream_config in "${streams[@]}"; do
    IFS=':' read -r video_file stream_name <<< "$stream_config"
    start_stream "$video_file" "$stream_name"
    sleep 1
done

echo -e "${GREEN}所有流已启动!${NC}"
echo ""
echo "RTSP URLs:"
for stream_config in "${streams[@]}"; do
    IFS=':' read -r video_file stream_name <<< "$stream_config"
    echo "  rtsp://${RTSP_SERVER}/live/${stream_name}"
done
echo ""
echo "VLC播放命令:"
echo "  vlc rtsp://${RTSP_SERVER}/live/traffic"
echo ""
echo "日志文件:"
echo "  /tmp/rtsp_*.log"
echo ""
echo -e "${YELLOW}按 Ctrl+C 停止所有推流${NC}"

# 优雅退出
cleanup() {
    echo ""
    echo "停止所有推流..."
    pkill -f "rtsp://${RTSP_SERVER}/live/"
    echo "完成!"
    exit 0
}

trap cleanup SIGINT SIGTERM

# 监控循环
while true; do
    sleep 5
    
    # 检查进程
    for stream_config in "${streams[@]}"; do
        IFS=':' read -r video_file stream_name <<< "$stream_config"
        
        if ! pgrep -f "rtsp://${RTSP_SERVER}/live/${stream_name}" > /dev/null; then
            echo -e "${RED}[$(date '+%H:%M:%S')] 流 ${stream_name} 已停止，重启中...${NC}"
            start_stream "$video_file" "$stream_name"
        fi
    done
done

